package com.hanlin.backend.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hanlin.backend.common.entity.HlUser;
import com.hanlin.backend.common.util.JwtUtil;
import com.hanlin.backend.service.HlUserService;
import com.hanlin.backend.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 描述：主要用来解析token，获取用户信息相关
 *
 * @author:hl.yuan
 * @date:2020-12-27
 */
@Service
@Slf4j
public class TokenServiceImpl implements TokenService {

    @Autowired
    private HlUserService hlUserService;

    @Override
    public HlUser getUserByRequest(HttpServletRequest request) {
        String token = request.getHeader(JwtUtil.TOKEN);
        if (StrUtil.isBlank(token)) {
            log.error("获取token为空");
            return null;
        }
        String username = JwtUtil.getUsername(token);
        HlUser byUserName = hlUserService.findByUserName(username);
        return byUserName;
    }
}
